<?php
namespace Home\Controller;
use Think\Controller;
class MemberController extends Controller 
{
	public function qqlogin()
	{
		// 取出这个openid对应的账号
		$memberModel = D('Admin/Member');
		$member = $memberModel->field('username')->where(array(
			'openid' => $_SESSION['openid'],
		))->find();
		// 如果有关联账号直接让这个账号登录 
		if($member)
		{
			$memberModel->$member['username'];
			if($memberModel->login(FALSE))
			{
					// 默认跳转地址
    				$returnUrl = U('/');  
    				// 如果session中有一个要跳转的地址就跳过去
    				$ru = session('returnUrl');
    				if($ru)
    				{
    					session('returnUrl', null);
    					$returnUrl = $ru;
    				}
    				$this->success('登录成功！',$returnUrl);
    				exit;
			}
			else 
				$this->error($memberModel->getError());
		}
		else 
			// 如果QQ号还没有关联账号那么就引导会员绑定一个账号
			redirect(U('login'));
	}

    /**
     * ajax验证是否登陆
     * @return [type] [description]
     */
	public function ajaxChkLogin()
	{   
		if(session('m_id'))
			echo json_encode(array(
				'login' => 1,
				'username' => session('m_username'),
			));
		else 
			echo json_encode(array(
				'login' => 0,
			));
	}
	// 制作验证码
	public function chkcode()
	{
		$Verify = new \Think\Verify(array(
		    'fontSize'    =>    30,    // 验证码字体大小
		    'length'      =>    3,     // 验证码位数
		    'useNoise'    =>    TRUE, // 关闭验证码杂点
		));
		$Verify->entry();
	}
    public function login()
    {
    	if(IS_POST)
    	{      
    		$model = D('Admin/Member');
    		if($model->validate($model->_login_validate)->create())
    		{
    			if($model->login())
    			{
    				// 默认跳转地址
    				$returnUrl = U('/');  
    				// 如果session中有一个要跳转的地址就跳过去
    				$ru = session('returnUrl');
    				if($ru)
    				{
    					session('returnUrl', null);
    					$returnUrl = $ru;
    				}
                    return show('1','登录成功',$returnUrl);
    			}
    		}
            return show('0',$model->getError());
    		
    	}
    	// 设置页面信息
    	$this->assign(array(
    		'_page_title' => '登录',
    		'_page_keywords' => '登录',
    		'_page_description' => '登录',
    	));
    	$this->display();
    }
    /**
     * 会员注册
     * @return [type] [description]
     */
    public function regist()
    {
    	if(IS_POST)
    	{      
            if (session('sjyz')!=I('post.mobile_code')) {
              return show('0','手机验证码错误');
            }
            
    		$model = D('Admin/Member');
    		if($model->create(I('post.'), 1))
    		{
    			if($model->add())
    			{
                    return show('1','注册成功');
    				
    			}
    		}
            return show('0',$model->getError());		
    	}
    	// 设置页面信息
    	$this->assign(array(
    		'_page_title' => '注册',
    		'_page_keywords' => '注册',
    		'_page_description' => '注册',
    	));
    	$this->display();
    }

    /**
     * 退出
     * @return [type] [description]
     */
	public function logout()
    {
    	$model = D('Admin/Member');
    	$model->logout();
    	redirect('/');
    }
    /**
     * 短信
     * @param   手机号
     * @return [type] [description]
     */
    public function msg(){
        $mobile=I('post.mobile');
        $appkey='23490444';
        $secret='34424c9413fbf0bf8a7204560a793151';
        require (VENDOR_PATH.'/alidayu/TopSdk.php');
 
        $c = new \TopClient;
        $c ->appkey =$appkey  ;
        $c ->secretKey = $secret ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest;
        $req ->setExtend( "123456" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "陈锦煊个人技术博" );
       
        $req->setSmsTemplateCode("SMS_21720135");
        $req ->setRecNum( $mobile );
        $rand=mt_rand(1000,9999);
        
        $req->setSmsParam("{\"yzm\":\"{$rand}\"}");
        $resp = $c ->execute( $req ); 
     
        if ($resp) {
            session('sjyz',$rand);
              return show('1','发送成功');
          }  
    }
    /**
     *ajax验证手机验证码
     *@param   sjyz 手机号
     * @return [type] [description]
     */
    public function sjyz(){
        if (session('sjyz')==I('get.sjyz')) {
           return show('1','手机验证码正确');
        }else{
            return show('0','手机验证码错误');
        }
    }
  
}





